<!-- 设备故障率统计 -->
<template>
  <div class="app-container">
    <div class="search-and-button" :class="{ showSearch: show }">
      <!-- <el-button type="primary" icon="el-icon-plus" @click="showDialog">新增</el-button> -->
      <ImportFile
        url="/purchase/statistics/rate/import"
        @getTableInfo="getTableInfo"
      />
      <el-button
        type="primary"
        plain
        :icon="!show ? 'el-icon-arrow-down' : 'el-icon-arrow-up'"
        @click="show = !show"
        >查询条件</el-button
      >
      <el-button type="primary" icon="el-icon-search" @click="getTableInfo()"
        >查询</el-button
      >
      <transition name="el-zoom-in-top">
        <div v-show="show" class="more-search">
          <el-form :model="search" label-width="80px">
            <el-row>
              <el-col :xs="24" :xl="6" :md="12">
                <el-form-item label="设备" prop="equipKeyword">
                  <el-input
                    v-model="search.equipKeyword"
                    placeholder="请输入设备名称或编码"
                    clearable
                    @keyup.enter.native="getTableInfo()"
                    @clear="getTableInfo()"
                  />
                </el-form-item>
              </el-col>
              <el-col :xs="24" :xl="12" :md="12">
                <el-form-item label="故障日期" prop="time">
                  <el-date-picker
                    v-model="search.time"
                    style="width: 100%"
                    type="daterange"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期"
                    format="yyyy-MM-dd"
                    value-format="yyyy-MM-dd"
                    placeholder="选择日期时间"
                    @change="getTableInfo()"
                  />
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </div>
      </transition>
    </div>
    <el-row class="table">
      <el-table
        v-loading="loading"
        :data="tableData"
        ref="tableData"
        stripe
        border
      >
        <el-table-column type="selection" align="center"></el-table-column>
        <el-table-column
          align="center"
          type="index"
          label="索引"
          width="60px"
        />
        <el-table-column
          v-for="item in fields"
          :align="item.align || 'left'"
          :prop="item.code.split(':')[1]"
          :label="item.val"
          :min-width="item.width + 'px'"
        >
          <template #default="{ row }">
            <div
              v-if="item.code.split(':')[1] === 'failureRate'"
              style="display: list-item"
            >
              <el-progress
                :percentage="Math.abs(row.failureRate)"
                :stroke-width="16"
                :color="customColors"
                :text-inside="true"
              />
            </div>
            <div v-else-if="item.code.split(':')[1] === 'checked'">
              <i
                v-if="row[item.code.split(':')[1]] == '是'"
                class="el-icon-check"
              />
              <i v-else class="el-icon-close" />
            </div>
            <span v-else>{{ row[item.code.split(":")[1]] }}</span>
          </template>
        </el-table-column>
      </el-table>
    </el-row>
    <el-row>
      <div class="table-btn-box">
        <el-button
          @click="
            () => {
              tableData.forEach((row) => {
                $refs['tableData'].toggleRowSelection(row, true);
              });
            }
          "
          >选择全部</el-button
        >
        <el-button
          @click="
            () => {
              tableData.forEach((row) => {
                $refs['tableData'].toggleRowSelection(row);
              });
            }
          "
          >反向选择</el-button
        >
        <el-button>
          更多菜单<i class="el-icon-arrow-down el-icon--right"></i>
        </el-button>
      </div>
      <pagination
        ref="Pagination"
        :total="total"
        :current.sync="search.current"
        :size.sync="search.size"
        @pagination="getTableInfo"
      />
    </el-row>
  </div>
</template>
<script>
import ImportFile from "@/components/ImportFile";
import Pagination from "@/components/Pagination";
export default {
  components: { Pagination, ImportFile },
  props: {
    fields: {
      type: Array,
      default: [],
    },
  },
  data() {
    return {
      loading: false,
      tableData: [],
      total: 0,
      search: {
        current: 1,
        size: 20,
        keyword: "",
        beginDate: "",
        endDate: "",
        equipKeyword: "",
        time: [],
      },
      receiptNoList: [],
      show: false,
      customColors: [
        { color: "#f56c6c", percentage: 30 },
        { color: "#e6a23c", percentage: 60 },
        { color: "#1989fa", percentage: 80 },
        { color: "#2ed573", percentage: 100 },
      ],
    };
  },
  mounted() {
    this.getTableInfo();
    this.getReceiptNo();
  },

  methods: {
    /* 获取所有收货订单 */
    async getReceiptNo() {
      const res = await this.$axios.post("/purchase/statistics/rate/receiptNo");
      if (res?.data.code === 200) {
        this.receiptNoList = res.data.data;
      }
    },

    /**
     * @description: page
     * @param {*} page
     * @return {*}
     */
    async getTableInfo(page = 1) {
      console.log(this.fields);
      this.search.current = page;
      const { time } = this.search;
      if (time && time.length) {
        this.search.beginDate = time[0];
        this.search.endDate = time[1];
      } else {
        this.search.beginDate = "";
        this.search.endDate = "";
      }
      const search = {
        ...this.search,
      };
      delete search.time;
      this.loading = true;
      const res = await this.$axios.post("/equip/failure/list", search);
      if (res?.data.code === 200) {
        this.tableData = res.data.data.list;
        this.total = res.data.data.total;
        setTimeout(() => {
          this.loading = false;
        }, 100);
      } else {
        this.loading = false;
      }
    },
  },
};
</script>
<style lang="scss" scoped>
.app-container {
  padding: 0px;
}
.search-and-button {
  min-height: 33px;
  line-height: 30px;
  height: 30px;
  overflow: hidden;
  transition: height 0.3s;
  width: 100%;
  .el-input {
    width: 350px;
    float: right;
  }
}
.showSearch {
  height: 95px;
}
.arrow {
  text-align: center;
  cursor: pointer;
  font-size: 13px;
}

.more-search {
  margin-top: 10px;
  background-color: #f5f7fa;
  border-radius: 10px;
  .el-form {
    padding: 10px 20px;
    .el-col {
      max-width: 380px;
      min-width: 150px;
      height: 40px;
      line-height: 40px;
      .el-input {
        width: 100%;
      }
    }
  }
}
.table {
  margin-top: 10px;
}
</style>
